package com.xiye.common.util;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.http.HttpServletResponse;
import com.xiye.common.entity.PageData;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @author 西野
 * @version V1.0
 * @title JsonFormat.java
 * @package com.xiye.util
 * @description JSON格式转换
 * @date 2018-07-12
 */
@SuppressWarnings({"unchecked", "rawtypes"})
public class JsonFormat {

    /*
     * @Author 西野
     * @Date 2018-06-22 16:06:06
     * @Description JSONArray转Map
     * @Param [jsonArray]
     * @Return java.util.Map<java.lang.String,java.lang.String>
     */
    public static Map<String, String> JsonArray_Map(JSONArray jsonArray) {
        List<Map<String, Object>> mapListJson = (List) jsonArray;
        Map<String, String> map = new HashMap<String, String>();
        for (int i = 0; i < mapListJson.size(); i++) {
            Map<String, Object> obj = mapListJson.get(i);
            for (Entry<String, Object> entry : obj.entrySet()) {
                String strkey = entry.getKey();
                Object strval = entry.getValue();
                map.put(strkey, strval.toString());
            }
        }
        return map;
    }

    /*
     * @Author 西野
     * @Date 2018-06-22 16:06:06
     * @Description JsonObject转Map
     * @Param [jsonObject]
     * @Return java.util.Map<java.lang.String,java.lang.String>
     */
    public static Map<String, String> JsonObject_Map(JSONObject jsonObject) {

        Map<String, String> map = new HashMap<String, String>();
        Map<String, Object> mapJson = JSONObject.fromObject(jsonObject);
        for (Entry<String, Object> entry : mapJson.entrySet()) {
            Object strkey = entry.getKey();
            Object strval = entry.getValue();
            map.put(strkey.toString(), strval.toString());
        }
        return map;
    }

    /*
     * @Author 西野
     * @Date 2018-06-22 16:06:06
     * @Description 手机访问json数据解码
     * @Param [pd]
     * @Return com.xiye.common.entity.PageData
     */
    public static PageData decodeURI(PageData pd) {
        JSONArray jsonArray = JSONArray.fromObject(pd);
        List<Map<String, Object>> mapListJson = (List) jsonArray;
        for (int i = 0; i < mapListJson.size(); i++) {
            Map<String, Object> obj = mapListJson.get(i);
            for (Entry<String, Object> entry : obj.entrySet()) {
                String strkey = entry.getKey();
                Object strval = entry.getValue();
                try {
                    strval = URLDecoder.decode((String) strval, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                pd.put(strkey, strval);
            }
        }
        return pd;
    }

    /*
     * @Author 西野
     * @Date 2018-06-22 16:06:06
     * @Description 平台返回json数据转码
     * @Param [jsonArray]
     * @Return net.sf.json.JSONArray
     */
    public static JSONArray encodeURI(JSONArray jsonArray) {
        List<Map<String, Object>> mapListJson = (List) jsonArray;
        JSONObject jsonArray_back = new JSONObject();
        JSONArray jsonArray_new = new JSONArray();
        for (int i = 0; i < mapListJson.size(); i++) {
            Map<String, Object> obj = mapListJson.get(i);
            for (Entry<String, Object> entry : obj.entrySet()) {
                Object strkey = entry.getKey();
                Object strval = entry.getValue();
                try {
                    String strval_replace = strval.toString();
                    strval_replace = strval_replace.replaceAll(" ", "%20");// 直接转码的话空格在手机端会变成“+”号，所以在转码前将空格替换成编码
                    strval = URLEncoder.encode(strval_replace, "UTF-8");
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                jsonArray_back.put(strkey, strval);
            }
            jsonArray_new.add(i, jsonArray_back);
        }
        return jsonArray_new;
    }

    public static void setResponseHeader(HttpServletResponse response) {
        response.setContentType("text/json");
        response.setHeader("Cache-Control", "no-cache");
        response.setCharacterEncoding("UTF-8");
    }
}
